import {myRequest} from "./api.js"

const get = (url, data) => {
	let token = uni.getStorageSync('openId')
	return myRequest({
		url: url,
		data: data,
		method: 'GET',
		header: { //自定义请求头信息
			"Content-Type": "application/json",
			'Authorization': token || ''
		}
	}).then((res) => {
		return res.data
	})
}

const post = (url, data) => {
	let token = uni.getStorageSync('openId')
	return myRequest({
		url: url,
		method: 'POST',
		data: data,
		header: { //自定义请求头信息
			"Content-Type": "application/json",
			'Authorization': token || ''
		}
	}).then((res) => {
		return res.data
	})
}

const  qrpost = async function() {
	// let token = uni.getStorageSync('openId')
	// return myRequest({
	// 	url: 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode',
	// 	method: 'POST',
	// 	data: data,
	// 	header: { //自定义请求头信息
	// 		"Content-Type": "application/json",
	// 		'Authorization': token || ''
	// 	}
	// }).then((res) => {
	// 	return res.data
	// })
	let actoken = await this.$get('/user/getAccessToken')
	let act = actoken.data.access_token
	return new Promise((resolve,reject) => {
			uni.request({
				url:'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=' + act ,
				method:"POST",
				data: {"path": "pages/circle/circle","width": 430}
			}).then((res)=>{
				resolve(res)
				console.log(res)
			}).catch((err)=>{
				reject(err)
				console.log(err)
			})
	})
}

const put = (url, data) => {
	let token = uni.getStorageSync('openId')
	return myRequest({
		url: url,
		method: 'PUT',
		data: data,
		header: { //自定义请求头信息
			'Authorization': token || ''
		}
	}).then((res) => {
		return res.data
	})
}

const remove = (url, data) => {
	let token = uni.getStorageSync('openId')
	return myRequest({
		url: url,
		method: 'DELETE',
		data: data,
		header: { //自定义请求头信息
			'Authorization': token || ''
		}
	}).then((res) => {
		return res.data
	})
}


module.exports = {
	get,
	post,
	put,
	remove,
	qrpost
}